package org.deng.vertx.springboot.web.socket;

import io.vertx.core.Handler;
import io.vertx.core.http.ServerWebSocket;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @author 邓力宾
 * @date 2022/12/29 13:41
 * @desc
 */
@Component
@Slf4j
public class WebSocketHandler implements Handler<ServerWebSocket> {


    @Override
    public void handle(ServerWebSocket webSocket) {
        String currID = webSocket.binaryHandlerID();
        log.info("连接成功:{}", currID);

        //　WebSocket 连接
        webSocket.frameHandler(handler -> {
            String textData = handler.textData();
            log.info("收到数据:{}", textData);
            webSocket.writeTextMessage("okkkkkkkkkk");
        });

        // 客户端WebSocket 关闭时，将当前ID从map中移除
        webSocket.closeHandler(handler -> {
            log.info("断开连接:{}", webSocket.binaryHandlerID());
        });

    }
}
